//<p>给你一个二叉树的根节点 <code>root</code> ，按 <strong>任意顺序</strong> ，返回所有从根节点到叶子节点的路径。</p>
//
//<p><strong>叶子节点</strong> 是指没有子节点的节点。</p>
//&nbsp;
//
//<p><strong>示例 1：</strong></p>
//<img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-tree.jpg" style="width: 207px; height: 293px;" />
//<pre>
//<strong>输入：</strong>root = [1,2,3,null,5]
//<strong>输出：</strong>["1-&gt;2-&gt;5","1-&gt;3"]
//</pre>
//
//<p><strong>示例 2：</strong></p>
//
//<pre>
//<strong>输入：</strong>root = [1]
//<strong>输出：</strong>["1"]
//</pre>
//
//<p>&nbsp;</p>
//
//<p><strong>提示：</strong></p>
//
//<ul>
//	<li>树中节点的数目在范围 <code>[1, 100]</code> 内</li>
//	<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
//</ul>
//<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>字符串</li><li>二叉树</li></div></div><br><div><li>👍 639</li><li>👎 0</li></div>

package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

public class _257_BinaryTreePaths {
    public static void main(String[] args) {
        Solution solution = new _257_BinaryTreePaths().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> result = new ArrayList<>();
            dfs(root, "", result);
            return result;
        }

        private void dfs(TreeNode root, String cur, List<String> result) {
            if (root ==null) {
                return;
            }
            cur = cur+root.val;
            if (root.left == null && root.right==null) {
                cur = cur.substring(0, cur.length()-2);
                result.add(cur);
                return;
            }

            dfs(root.left, cur+"->", result);
            dfs(root.right, cur+"->", result);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}